﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System.IO;
using ReflectionStudio.Core;
using Intex.Tools;


namespace WPFExcelReport
{
    public partial class JLTONAV95Data
    {
        DataTable tbICC_ABAUFM;
        private void ReOrgnizeOrderStructure()
        {

            foreach (DataRow dwABPOS_EXT in SetJob.Tables["ABPOS_EXT"].Rows)
            {

            }

        }
        public DataSet ConvertToIntex()
        {
            DataSet setResult = new DataSet();
            DataSet Return = new DataSet();//is used to magage the data to upload to intex 
            string krs = "", lfd = "", pos = "";
            pABPAUFLFD = new Dictionary<string, string>();
            try
            {
                if (tbICC_ABAUFM == null)
                {
                    tbICC_ABAUFM = SetJob.Tables["ICC_ABAUFM"];
                }
                //Assign ABPAUFLFD
                ReOrgnizeOrderStructure();
                foreach (DataRow dwABPOS_EXT in SetJob.Tables["ABPOS_EXT"].Rows)
                {
                    Set4ITX.Clear();
                    if (Set4ITX.Tables.Contains("ABPOS"))
                    {
                        Set4ITX.Tables.Remove("ABPOS");
                    }
                    SetExt.Clear();
                    SetExt.Merge(SetMap);
                    SetExt.Merge(SetJob);
                    if (!SetExt.Tables.Contains("ABAUFM_ITX"))
                    {
                        DataTable tbABA = tbICC_ABAUFM.Clone();
                        tbABA.TableName = "ABAUFM_ITX";
                        SetExt.Merge(tbABA);
                    }
                    if (!SetExt.Tables.Contains("APFOLGE_ITX"))
                    {
                        DataTable tbABF = SetExt.Tables["APFOLGE"].Clone();
                        tbABF.TableName = "APFOLGE_ITX";
                        SetExt.Merge(tbABF);
                    }
                    if (!SetExt.Tables.Contains("APARTK_ITX"))
                    {
                        DataTable tbABF = SetExt.Tables["APARTK"].Clone();
                        tbABF.TableName = "APARTK_ITX";
                        SetExt.Merge(tbABF);
                    }
                    Convert2Intex = new Convert2Intex(SetExt, dwIntexStd, OracleDB, proxy);
                    dwIntexStd = Convert2Intex.dwIntexStd;
                    string ABPAUFTYP = "";
                    DataRow dwData = null;
                    DataRow dwABPOS = null;
                    if (SetExt.Tables.Contains("ABPOS_EXT"))
                    {
                        SetExt.Tables["ABPOS_EXT"].Clear();
                        dwData = SetExt.Tables["ABPOS_EXT"].NewRow();
                        dwData.ItemArray = dwABPOS_EXT.ItemArray;
                        SetExt.Tables["ABPOS_EXT"].Rows.Add(dwData);
                    }
                    else
                    {
                        DataTable tb = SetJob.Tables["ABPOS_EXT"].Clone();
                        dwData = tb.NewRow();
                        dwData.ItemArray = dwABPOS_EXT.ItemArray;
                        tb.Rows.Add(dwData);
                        SetExt.Merge(tb);
                    }
                    krs = dwABPOS_EXT["ABPAUFKRS"].ToString();
                    lfd = dwABPOS_EXT["ABPAUFLFD"].ToString();
                    pos = dwABPOS_EXT["ABPAUFPOS"].ToString();
                    time = Right("00" + System.DateTime.Now.Hour, 2).ToString() + Right("00" + System.DateTime.Now.Minute, 2).ToString();
                    ABPAUFTYP = dwABPOS_EXT["ABPAUFART"].ToString();
                    switch (ABPAUFTYP)
                    {
                        case "VA":
                            CreateAB05(dwABPOS_EXT);
                            break;
                        case "DI":
                            CreateAB55(dwABPOS_EXT);
                            break;
                        case "VW":
                            CreateAB45(dwABPOS_EXT);
                            break;
                        case "GP":
                            CreateAB50(dwABPOS_EXT);
                            break;
                    }
                    if (Set4ITX.Tables.Contains("ABPOS_ITX"))
                    {
                        Set4ITX.Tables["ABPOS_ITX"].TableName = "ABPOS";
                    }
                    Upload2Intex();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                EventDispatcher.Instance.RaiseStatus("程序遇到如下问题:" + ex.Message, StatusEventType.StopProgress);
            }
            return Return;
        }
        private bool GetABPNum(ref DataRow dwABPOS_EXT)
        {
            bool newabk = false;
            string KRS = "", LFD = "", LFD0 = "", POS = "", POS0 = "";
            DataSet SResult = new DataSet();
            string filter = "";
            DataRow[] rows;
            KRS = dwABPOS_EXT["ABPAUFKRS"].ToString();
            LFD0 = dwABPOS_EXT["ABPAUFLFD"].ToString();
            POS0 = dwABPOS_EXT["ABPAUFPOS"].ToString();
            if (!pABPAUFLFD.ContainsKey(KRS + LFD0))
            {
                LFD = GetAUFLFD(KRS, "ABKOPF");
                pABPAUFLFD.Add(KRS + LFD0, LFD);
                newabk = true;
            }
            else
            {
                LFD = pABPAUFLFD[KRS + LFD0];
            }
            DataRow dwData = INTEXDB.ABPOS.NewRow();
            dwData["ABPMANDANT"] = "00";
            dwData["ABPAUFKRS"] = KRS;
            dwData["ABPAUFLFD"] = LFD;
            SResult = INTEXDBC.QueryABPOS(dwData, 19);
            POS = "000";
            foreach (DataRow RX in SResult.Tables[0].Rows)
            {
                POS = RX["ABPAUFPOS"].ToString();
                if (POS.Trim() == "")
                {
                    POS = "000";
                }
                POS = Right("000" + (Convert.ToInt16(POS) + 1), 3);
            }

            dwABPOS_EXT["ABPAUFLFD"] = LFD;
            dwABPOS_EXT["ABPAUFPOS"] = POS;
            filter = "ABAAUFTYP='V' AND ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD0 + "' AND ABAAUFPOS='" + POS0 + "'";
            rows = tbICC_ABAUFM.Select(filter);
            foreach (DataRow row in rows)
            {
                row["ABAAUFLFD"] = LFD;
                row["ABAAUFPOS"] = POS;
            }
            filter = "ABAVKAUFTYP='V' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD0 + "' AND ABAVKAUFPOS='" + POS0 + "'";
            rows = tbICC_ABAUFM.Select(filter);
            foreach (DataRow row in rows)
            {
                row["ABAVKAUFLFD"] = LFD;
                row["ABAVKAUFPOS"] = POS;
            }
            return newabk;

        }
        public void ConsoldateFlow(DataRow dwABPOS_ITX, string AUFTYP)
        {

            string pflow = "";
            DataSet STemp = null;
            DataTable tbABF = null;
            DataRow dwABF = null;
            DataSet SResult = new DataSet();
            string filter = "";
            DataRow[] rows;
            string ABPANRKR = dwABPOS_ITX["ABPANRKR"].ToString();
            string ABPALFDNR = dwABPOS_ITX["ABPALFDNR"].ToString();
            filter = "APFANRKRS='" + ABPANRKR + "' AND APFALFDNR='" + ABPALFDNR + "'";
            rows = SetExt.Tables["APFOLGE"].Select(filter);
            int nstep = 0;
            foreach (DataRow row in rows)
            {
                string step = row["APFAFOLGE"].ToString();
                if (pflow.Contains(ABPANRKR + ABPALFDNR + step + ";"))
                    continue;
                pflow += ABPANRKR + ABPALFDNR + step + ";";
                nstep++;
                row["APFAFOLGE"] = Right("000" + nstep * 10, 3);
                if (SetExt.Tables.Contains("APFOLGE_ITX"))
                {
                    SetExt.Tables["APFOLGE_ITX"].Clear();
                }
                tbABF = SetExt.Tables["APFOLGE_ITX"];
                dwABF = tbABF.NewRow();
                dwABF.ItemArray = row.ItemArray;
                tbABF.Rows.Add(dwABF);

                if (SetExt.Tables.Contains("APARTK"))
                {
                    filter = "APAKANRKRS='" + ABPANRKR + "' AND APAKALFDNR= '" + ABPALFDNR + "' AND APAKAFOLGE='" + step + "'";
                    DataRow[] rx1 = SetExt.Tables["APARTK"].Select(filter);
                    foreach (DataRow rx in rx1)
                    {
                        if (SetExt.Tables.Contains("APARTK_ITX"))
                        {
                            SetExt.Tables["APARTK_ITX"].Clear();
                        }
                        tbABF = SetExt.Tables["APARTK_ITX"];
                        dwABF = tbABF.NewRow();
                        dwABF.ItemArray = row.ItemArray;
                        tbABF.Rows.Add(dwABF);
                        break;
                    }
                }
                STemp = Convert2ABFOLGE(dwABPOS_ITX);
                DataSet STP;
                int ABEEINSTNR = 0;
                foreach (DataRow rflow in STemp.Tables[0].Rows)
                {
                    switch (AUFTYP)
                    {
                        case "VA":
                            rflow["ABFSOLLSTK"] = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE8"].ToString()) / 100;
                            rflow["ABFSOLLME"] = 1;
                            rflow["ABFZUSATZ2"] = "B";
                            rflow["ABFMEKOPFKZ"] = " ";

                            break;
                        case "DI":
                            rflow["ABFSOLLSTK"] = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE8"].ToString()) / 100;
                            rflow["ABFSOLLME"] = 2;
                            rflow["ABFSOLLMTR"] = rflow["ABFSOLLMG"];
                            rflow["ABFSPSTA"] = 0;
                            rflow["ABFSPGRD"] = " ";
                            rflow["ABFMEKOPFKZ"] = 2;
                            break;
                        case "VW":
                            rflow["ABFSOLLSTK"] = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE8"].ToString()) / 100;
                            rflow["ABFSOLLME"] = 2;
                            rflow["ABFMEKOPFKZ"] = "2";
                            rflow["ABFSOLLMTR"] = rflow["ABFSOLLMG"];
                            break;
                        case "GP":
                            rflow["ABFSOLLMG"] = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE5"].ToString()) * 10;
                            rflow["ABFSOLLME"] = 5;
                            rflow["ABFSOLLMTR"] = 0;
                            rflow["ABFFIX"] = "J";
                            rflow["ABFFZUSATZKZ1"] = "F";
                            rflow["ABFSPSTA"] = "N";
                            rflow["ABFSPGRD"] = " ";
                            rflow["ABFMEKOPFKZ"] = "1";
                            break;
                        default:
                            break;
                    }
                    ABEEINSTNR++;
                    STP = Convert2ABARTE(rflow);
                    foreach (DataRow RTP in STP.Tables[0].Rows)
                    {
                        RTP["ABEEINSTNR"] = Right("000" + ABEEINSTNR * 10, 3);
                    }
                    Set4ITX.Merge(STP);
                    if (ABEEINSTNR == 1)
                    {
                        foreach (DataRow RTP in STP.Tables[0].Rows)
                        {
                            RTP["ABEEINSTNR"] = "000";
                            RTP["ABEWERTX"] = "                ";
                            RTP["ABEWERTN"] = "0000000000000000";
                            RTP["ABEPROGNR"] = "0000";
                            RTP["ABEEINAUS"] = " ";
                            RTP["ABEREZEPTKZ"] = " ";
                            RTP["ABEREZEPTNR"] = "          ";
                            RTP["ABEREZEPTST"] = "0";
                            RTP["ABEWERKZ"] = "              ";
                            RTP["ABEFAKTOR"] = "00";
                            RTP["ABESTAKZ"] = " ";
                            RTP["ABEMODEIN"] = "E";
                            RTP["ABEIWERTX"] = "                ";
                            RTP["ABEIWERTN"] = "0000000000000000";
                            RTP["ABEPFLKZ"] = " ";
                            RTP["ABEDRUCKKZ"] = " ";

                            RTP["ABERUEKZ"] = " ";
                            RTP["ABEREZPOS"] = "0000";
                            RTP["ABEMETHODE"] = "     ";
                            RTP["ABEMWERT"] = " ";
                            RTP["ABEMESCHL"] = "00";
                            RTP["ABEFILLER"] = "                  ";
                        }
                        Set4ITX.Merge(STP);
                    }
                }
                SResult.Merge(STemp);
            }
            var ql = from DataRow row in SResult.Tables["ABFOLGE"].AsEnumerable()
                     where row["ABFLIEFNR"].ToString() != "00000000"
                     select row;
            foreach (DataRow row in ql)
            {
                //ABFSOLLMG  ABFSOLLMTR
                string ME = row["ABFSOLLME"].ToString();
                switch (ME)
                {
                    case "1":
                        row["ABFSOLLMG"] = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE1"].ToString()) * 10;
                        row["ABFSOLLMTR"] = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE1"].ToString()) * 10;
                        break;
                }
                dwABPOS_ITX["ABPLIEFNR"] = row["ABFLIEFNR"].ToString();
            }
            Set4ITX.Merge(SResult);


        }
        public void DeleteReservaton(DataRow dwInput, DataRow dwABAUFM, string AUFTYP)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dw001 = dwInput;
            DataRow dwNext = null;
            DataTable tbX = new DataTable();
            if (dwInput == null)
                return;
            if (dwABAUFM == null)
                return;
            decimal ABARSTSLG = Convert.ToDecimal(dwInput["ABARSTSLG"].ToString());
            decimal ABARSTSGEW = Convert.ToDecimal(dwInput["ABARSTSGEW"].ToString());
            decimal ABASGESLG = Convert.ToDecimal(dwInput["ABASGESLG"].ToString());
            decimal ABASGESGEW = Convert.ToDecimal(dwInput["ABASGESGEW"].ToString());
            decimal ABASANZSTK = Convert.ToDecimal(dwABAUFM["ABASANZSTK"].ToString());
            decimal ABASSTKLG = Convert.ToDecimal(dwABAUFM["ABASSTKLG"].ToString());
            decimal ABARESMG = ABASANZSTK * ABASSTKLG;
            switch (AUFTYP)
            {
                case "VA":
                    if (ABARSTSLG + ABARESMG > ABASGESLG)
                    {
                        dwInput["ABARSTSLG"] = ABASGESLG;
                        dwInput["ABARSTSGEW"] = ABASGESGEW;
                    }
                    else
                    {
                        dwInput["ABARSTSLG"] = ABARSTSLG + ABARESMG;
                        dwInput["ABARSTSGEW"] = ABARSTSGEW + ABARESMG * (ABARSTSGEW / ABARSTSLG);
                    }
                    INTEXDBC.UpdateABAUFM(dwInput, 1);
                    INTEXDBC.DeleteABAUFM(dwABAUFM, 1);
                    break;
            }
        }
        public DataSet NewReservaton(DataRow dwInput, ref DataRow dwABAUFM, string AUFTYP)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dw001 = dwInput;
            DataRow dwNext = null;
            DataTable tbX = new DataTable();
            if (dwInput == null)
            {
                DataRow dwData = INTEXDB.ABAUFM.NewRow();
                dwData["ABAMANDANT"] = dwABAUFM["ABAMANDANT"];
                dwData["ABAAUFKRS"] = dwABAUFM["ABAAUFKRS"];
                dwData["ABAAUFLFD"] = dwABAUFM["ABAAUFLFD"];
                dwData["ABAAUFPOS"] = dwABAUFM["ABAAUFPOS"];
                dwData["ABAAPOS"] = "0001";
                SResult = INTEXDBC.QueryABAUFM(dwABAUFM, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwInput = row;
                }
            }
            if (dwInput == null)
                return SReturn;
            tbX = INTEXDB.ABAUFM.Clone();
            dw001 = tbX.NewRow();
            dw001.ItemArray = dwInput.ItemArray;
            /*tbX.Rows.Add(dw001);
            SReturn.Merge(tbX);
            return SReturn;*/
            dwNext = tbX.NewRow();
            dwNext.ItemArray = dwInput.ItemArray;

            DataRow dwWDECK = INTEXDB.WDECK.NewRow();

            decimal ABALEAPOS = Convert.ToDecimal(dwInput["ABALEAPOS"].ToString());
            dw001["ABALEAPOS"] = ABALEAPOS + 1;
            dw001["ABANEUBEN"] = dwABAUFM["ABAUSER"];
            dw001["ABANEUDAT"] = dwABAUFM["ABADATE"];
            dwNext["ABAAPOS"] = Right("0000" + dw001["ABALEAPOS"].ToString(), 4);

            decimal ABASGESLG = Convert.ToDecimal(dwInput["ABASGESLG"].ToString());
            decimal ABASGESGEW = Convert.ToDecimal(dwInput["ABASGESGEW"].ToString());
            decimal ABARSTSLG = Convert.ToDecimal(dwInput["ABARSTSLG"].ToString());
            decimal ABARESMG = Convert.ToDecimal(dwABAUFM["ABARESMG"].ToString());
            decimal ABASSTKLG = Convert.ToDecimal(dwABAUFM["ABASSTKLG"].ToString());
            if (ABASSTKLG == 0)
                ABASSTKLG = 1;
            decimal ABASANZSTK = Math.Round(ABARESMG * 10 / ABASSTKLG, 0);
            if (ABASANZSTK == 0)
                ABASANZSTK = 1;
            ABASSTKLG = Math.Round(ABARESMG * 10 / ABASANZSTK);
            ABARESMG = ABASANZSTK * ABASSTKLG;
            ABARSTSLG = ABARSTSLG - ABARESMG;
            if (ABARSTSLG < 0)
            {
                ABARSTSLG = 0;
            }

            switch (AUFTYP)
            {
                case "VA":
                    #region======VA==========

                    dw001["ABARSTSLG"] = ABARSTSLG;
                    dw001["ABARSTSGEW"] = ABARSTSLG * (ABASGESGEW / ABASGESLG);
                    dwABAUFM["ABARESMG"] = ABARESMG;
                    dwABAUFM["ABASSTKLG"] = ABASSTKLG;
                    dwABAUFM["ABASANZSTK"] = ABASANZSTK;
                    dwNext["ABASSTKLG"] = ABASSTKLG;
                    dwNext["ABASANZSTK"] = ABASANZSTK;
                    dwNext["ABASSTKLGV"] = 100;
                    dwNext["ABASSTKLGB"] = 999000;
                    dwNext["ABASGESLG"] = 0;
                    dwNext["ABASGESGEW"] = 0;
                    dwNext["ABASME"] = " ";
                    dwNext["ABARSTSLG"] = 0;
                    dwNext["ABARSTSGEW"] = 0;
                    dwNext["ABALEAPOS"] = 0;
                    dwNext["ABAVKAUFTYP"] = dwABAUFM["ABAVKAUFTYP"];
                    dwNext["ABAVKAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwNext["ABAVKAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwNext["ABAVKAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwNext["ABAVKUPOS"] = dwABAUFM["ABAVKUPOS"];
                    dwNext["ABAVB"] = dwABAUFM["ABAVB"];
                    dwNext["ABAVG"] = dwABAUFM["ABAVG"];
                    dwNext["ABAZUPRIO"] = 9 - ABALEAPOS - 1;
                    dwNext["ABARVSACHB"] = dwABAUFM["ABARVSACHB"];
                    dwNext["ABARKUNDNR"] = dwABAUFM["ABARKUNDNR"];
                    dwNext["ABAVG"] = dwABAUFM["ABAVG"];
                    dwNext["ABAVLVB"] = dwABAUFM["ABAVLVB"];
                    dwNext["ABAVLVG"] = dwABAUFM["ABAVLVG"];
                    dwNext["ABAVLSACHB"] = dwABAUFM["ABARVSACHB"];
                    dwNext["ABAPRKUNDNR"] = dwABAUFM["ABARKUNDNR"];
                    dwNext["ABAPRAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwNext["ABAPRAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwNext["ABAPRAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwNext["ABAVERSDAT"] = dwABAUFM["ABAVKSTARTD"];
                    dwNext["ABAVAKZ"] = " ";
                    dwNext["ABAAUFMSL"] = dwABAUFM["ABAAUFMSL"];
                    dwNext["ABAWOBST"] = "I";
                    dwNext["ABAASSCHNIT"] = " ";
                    dwNext["ABANEUBEN"] = dwABAUFM["ABAUSER"];
                    dwNext["ABANEUDAT"] = dwABAUFM["ABADATE"];
                    #endregion
                    break;
                case "DI":
                    #region======DI==========

                    dw001["ABARSTSLG"] = ABARSTSLG;
                    dw001["ABARSTSGEW"] = ABARSTSLG * (ABASGESGEW / ABASGESLG);
                    dwABAUFM["ABARESMG"] = 0;
                    dwABAUFM["ABASSTKLG"] = ABASSTKLG;
                    dwABAUFM["ABASANZSTK"] = ABASANZSTK;
                    dwNext["ABASSTKLG"] = ABASSTKLG;
                    dwNext["ABASANZSTK"] = ABASANZSTK;
                    dwNext["ABARSTSLG"] = 0;
                    dwNext["ABARSTSGEW"] = 0;
                    dwNext["ABASGESLG"] = 0;
                    dwNext["ABASGESGEW"] = 0;
                    dwNext["ABASSTKLGV"] = 100;
                    dwNext["ABASSTKLGB"] = 999000;
                    dwNext["ABASGESLG"] = 0;
                    dwNext["ABASGESGEW"] = 0;
                    dwNext["ABASME"] = " ";
                    dwNext["ABALEAPOS"] = 1;
                    dwNext["ABAVKAUFTYP"] = dwABAUFM["ABAVKAUFTYP"];
                    dwNext["ABAVKAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwNext["ABAVKAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwNext["ABAVKAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwNext["ABAVKUPOS"] = dwABAUFM["ABAVKUPOS"];
                    dwNext["ABAZUPRIO"] = 9 - ABALEAPOS - 1;
                    dwNext["ABAVAKZ"] = " ";
                    dwNext["ABAWOBST"] = "I";
                    dwNext["ABAASSCHNIT"] = " ";
                    dwNext["ABANEUBEN"] = dwABAUFM["ABAUSER"];
                    dwNext["ABANEUDAT"] = dwABAUFM["ABADATE"];
                    dwWDECK["WDMANDANT"] = dwInput["ABAMANDANT"];
                    dwWDECK["WDSA"] = dwABAUFM["ABAVKAUFTYP"];
                    dwWDECK["WDAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwWDECK["WDAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwWDECK["WDAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwWDECK["WDUPOS"] = "01";
                    dwWDECK["WDTYP"] = "B";
                    SResult = INTEXDBC.QueryWDECK(dwWDECK, 5);
                    foreach (DataRow rx in SResult.Tables[0].Rows)
                    {
                        rx["WDRESERV"] = ABARESMG;
                        INTEXDBC.UpdateWDECK(rx, "", 1);
                    }
                    #endregion
                    break;
                case "VW":
                    #region======VW==========
                    dw001["ABASME"] = "1";
                    dwABAUFM["ABARESMG"] = 0;
                    dwABAUFM["ABASSTKLG"] = ABASSTKLG*100;
                    dwABAUFM["ABASANZSTK"] = ABASANZSTK/100;
                    dwNext["ABASSTKLG"] = ABASSTKLG*100;
                    dwNext["ABASANZSTK"] = ABASANZSTK/100;
                    dwNext["ABARSTSLG"] = 0;
                    dwNext["ABARSTSGEW"] = 0;
                    dwNext["ABASGESLG"] = 0;
                    dwNext["ABASGESGEW"] = 0;
                    dwNext["ABASSTKLGV"] = 100;
                    dwNext["ABASSTKLGB"] = 9999000;
                    dwNext["ABASGESLG"] = 0;
                    dwNext["ABASGESGEW"] = 0;
                    dwNext["ABASME"] = " ";
                    dwNext["ABALEAPOS"] = 1;
                    dwNext["ABAVKAUFTYP"] = dwABAUFM["ABAVKAUFTYP"];
                    dwNext["ABAVKAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwNext["ABAVKAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwNext["ABAVKAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwNext["ABAVKUPOS"] =Right(dwABAUFM["ABAVKUPOS"].ToString(),2);
                    dwNext["ABAZUPRIO"] = 9 - ABALEAPOS - 1;
                    dwNext["ABAVAKZ"] = " ";
                    dwNext["ABABA"] = "  ";
                    
                    dwNext["ABAWOBST"] = " ";
                    dwNext["ABAASSCHNIT"] = " ";
                    dwNext["ABANEUBEN"] = dwABAUFM["ABAUSER"];
                    dwNext["ABANEUDAT"] = dwABAUFM["ABADATE"];
                    DataRow dwData = INTEXDB.ABPOS.NewRow();
                    dwData["ABPMANDANT"] = dwInput["ABAMANDANT"];                    
                    dwData["ABPAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwData["ABPAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwData["ABPAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    SResult = INTEXDBC.QueryABPOS(dwData, 1);
                    foreach (DataRow rx in SResult.Tables[0].Rows)
                    { 
                        rx["ABPRESMGE"] = ABARESMG;
                        rx["ABPRESKG"] = 0-Convert.ToDecimal(rx["ABPSOLLMENGE5"].ToString()) * 10;
                        rx["ABPRESSTK"] = ABASANZSTK;
                        INTEXDBC.UpdateABPOS(rx, 4);
                    }
                    dwData = INTEXDB.BAAUF.NewRow();
                    dwData["BAMANDANT"] = dwInput["ABAMANDANT"];                    
                    dwData["BAAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwData["BAAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwData["BAAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwData["BAPOS"] = dwABAUFM["ABAVKUPOS"];
                    SResult = INTEXDBC.QueryBAAUF(dwData, 3);
                    foreach (DataRow rx in SResult.Tables[0].Rows)
                    {  
                        rx["BAMTR2"] = ABARESMG;
                        rx["BAANZ2"] = ABASANZSTK / 100;
                        INTEXDBC.UpdateBAAUF(rx,"", 2);
                    }
                    #endregion
                    break;
                case "GP":
                    #region======GP==========
                    dw001["ABASME"] = " ";
                    dw001["ABARSTSLG"] = "0";
                    decimal ABARSTSGEW = Convert.ToDecimal(dw001["ABARSTSGEW"].ToString());
                    dw001["ABARSTSGEW"] = ABARSTSGEW - ABARESMG*10;                   
    
                    dwNext["ABARESMG"] = ABARESMG * 10;
                    dwNext["ABASANZSTK"] = 1;  
                    dwNext["ABARSTSLG"] = 0;
                    dwNext["ABASSTKLGV"] = 0;
                    dwNext["ABASSTKLGB"] = 0;
                    dwNext["ABASSTKGEW"] = ABARESMG*10;
 
                    dwNext["ABASME"] = " ";
                    dwNext["ABALEAPOS"] = 1;
                    dwNext["ABAVKAUFTYP"] = dwABAUFM["ABAVKAUFTYP"];
                    dwNext["ABAVKAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwNext["ABAVKAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwNext["ABAVKAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwNext["ABAVKUPOS"] = dwABAUFM["ABAVKUPOS"];
                    dwNext["ABAZUPRIO"] = 9 - ABALEAPOS - 1;
                    dwNext["ABAVAKZ"] = " ";
                    dwNext["ABAWOBST"] = " ";
                    dwNext["ABAASSCHNIT"] = " ";
                    dwNext["ABANEUBEN"] = dwABAUFM["ABAUSER"];
                    dwNext["ABANEUDAT"] = dwABAUFM["ABADATE"];

                    DataRow dwGAAUF = INTEXDB.GAAUF.NewRow();
                    dwGAAUF["GAMANDANT"] = dwInput["ABAMANDANT"];                
                    dwGAAUF["GAAUFKRS"] = dwABAUFM["ABAVKAUFKRS"];
                    dwGAAUF["GAAUFLFD"] = dwABAUFM["ABAVKAUFLFD"];
                    dwGAAUF["GAAUFPOS"] = dwABAUFM["ABAVKAUFPOS"];
                    dwGAAUF["GAPOS"] = dwABAUFM["ABAVKUPOS"];
                    SResult = INTEXDBC.QueryGAAUF(dwGAAUF, 5);
                    foreach (DataRow rx in SResult.Tables[0].Rows)
                    {
                        rx["GAKG2"] = ABARESMG;
                        INTEXDBC.UpdateGAAUF(rx, "",2);
                    }
                    #endregion
                    break;
            }
            tbX.Rows.Add(dw001);
            tbX.Rows.Add(dwNext);
            SReturn.Merge(tbX);
            return SReturn;
        }
        /// <summary>
        /// 1.获取新的生产单号码，将新的生产号码更新现在ICC_ABAUFM
        /// 2.获取行号后，将行号更新
        /// 3.根据ICC_ABAUFM建立的AV95
        /// 4.根据ICC_ABAUFM产生原料需求
        /// </summary>
        /// <param name="dwABPOS_EXT"></param>
        public void CreateAB05(DataRow dwABPOS_EXT)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            DataTable tbNABPOS = new DataTable();
            DataTable tbABA = null;

            bool newabk = false;
            DataRow nwABPOS;

            string VKPKRS = "  ", VKPLFD = "000000", VKPTYE = " ", VKPPOS = "000";

            string filter = "";
            DataRow[] rows;
            DataRow dwABAUFM_ITX = null;
            string KRS = "", LFD = "", POS = "";
            newabk = GetABPNum(ref dwABPOS_EXT);
            KRS = dwABPOS_EXT["ABPAUFKRS"].ToString();
            LFD = dwABPOS_EXT["ABPAUFLFD"].ToString();
            POS = dwABPOS_EXT["ABPAUFPOS"].ToString();
            if (SetExt.Tables.Contains("ABAUFM_ITX"))
            {
                tbABA = SetExt.Tables["ABAUFM_ITX"];
            }
            #region=====ABPOS=========
            EventDispatcher.Instance.RaiseStatus("转换ABPOS", StatusEventType.RunningProgress);
            SResult = Convert2ABPOS(dwABPOS_EXT);
            nwABPOS = SResult.Tables[0].Rows[0];
            DataRow dwABPOS_ITX = null;
            if (SetExt.Tables.Contains("ABPOS_ITX"))
            {
                SetExt.Tables.Remove("ABPOS_ITX");
            }
            DataTable tbABPOS_ITX = SResult.Tables[0];
            tbABPOS_ITX.TableName = "ABPOS_ITX";
            SetExt.Merge(tbABPOS_ITX);
            dwABPOS_ITX = SetExt.Tables["ABPOS_ITX"].Rows[0];
            if (newabk)
            {
                SResult = Convert2ABKOPF(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion
            #region=====ABPR=========
            EventDispatcher.Instance.RaiseStatus("转换ABPR", StatusEventType.RunningProgress);
            SResult = Convert2ABPR(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion
            #region=====ABAUFM=========
            EventDispatcher.Instance.RaiseStatus("转换Convert2ABAUFM", StatusEventType.RunningProgress);
            SResult = Convert2ABAUFMVA01(dwABPOS_ITX);
            DataRow dw001 = SResult.Tables[0].Rows[0];
            //Set4ITX.Merge(SResult);
            filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            if (rows.Length == 0)
            {
                Set4ITX.Merge(SResult);
            }
            foreach (DataRow row in rows)
            {
                dwABAUFM_ITX = tbICC_ABAUFM.Clone().NewRow();
                dwABAUFM_ITX.ItemArray = row.ItemArray;
                SResult = NewReservaton(dw001, ref dwABAUFM_ITX, "VA");
                Set4ITX.Merge(SResult);

                VKPKRS = dwABAUFM_ITX["ABAVKAUFKRS"].ToString();
                VKPLFD = dwABAUFM_ITX["ABAVKAUFLFD"].ToString();
                VKPPOS = dwABAUFM_ITX["ABAVKAUFPOS"].ToString();
                filter = "VKPAUFKRS='" + VKPKRS + "' AND VKPAUFLFD='" + VKPLFD + "' AND VKPAUFPOS='" + VKPPOS + "'";
                rows = SetExt.Tables["VKPOS"].Select(filter);
                foreach (DataRow dwVKPOS in rows)
                {
                    decimal ABASANZSTK = Convert.ToDecimal(dwABAUFM_ITX["ABASANZSTK"].ToString());
                    decimal VKPLAEBIS = Convert.ToDecimal(dwABAUFM_ITX["ABASSTKLG"].ToString());
                    dwVKPOS["VKPPRODST"] = "M";
                    dwVKPOS["VKPRESERV"] = dwABAUFM_ITX["ABARESMG"];//*0.9144
                    dwVKPOS["VKPRESERVT1"] = ABASANZSTK * 100;
                    dwVKPOS["VKPRESERVT2"] = dwABAUFM_ITX["ABARESMG"];//*0/1
                    dwVKPOS["VKPRESERVT3"] = dwABPOS_ITX["ABPSOLLMENGE4"];
                    dwVKPOS["VKPRESERVT4"] = dwABPOS_ITX["ABPSOLLMENGE6"];
                }
                DataTable tbTemp = rows.CopyToDataTable();
                tbTemp.TableName = "VKPOS";
                Set4ITX.Merge(tbTemp);
            }

            #endregion
            #region=====ABFOLGE=========
            ConsoldateFlow(dwABPOS_ITX, "VA");
            #endregion
            #region=====WDECKZ=========
            EventDispatcher.Instance.RaiseStatus("转换Convert2FDECKZ", StatusEventType.RunningProgress);
            SResult = Convert2WDECKZ(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion
            #region=====WDECKB=========
            filter = "ABAWAKZ='R' AND ABAVKAUFTYP='V' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "'AND ABAVKAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);

            foreach (DataRow row in rows)
            {
                if (SetExt.Tables.Contains("ABAUFM_ITX"))
                {
                    SetExt.Tables["ABAUFM_ITX"].Clear();
                }
                tbABA = SetExt.Tables["ABAUFM_ITX"];
                DataRow rx = tbABA.NewRow();
                rx.ItemArray = row.ItemArray;
                tbABA.Rows.Add(rx);
                SetExt.Merge(tbABA);
                SResult = Convert2WDECKB(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion
            Set4ITX.Merge(SetExt.Tables["ABPOS_ITX"]);
        }
        public void CreateAB55(DataRow dwABPOS_EXT)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tbABA = null;
            bool newabk = false;
            DataRow nwABPOS;

            string VKPKRS = "  ", VKPLFD = "000000", VKPTYE = " ", VKPPOS = "000";
            string processed = "";
            string filter = "";
            DataRow[] rows;
            DataRow dwABAUFM_ITX = null;
            string KRS = "", LFD = "", POS = "";
            newabk = GetABPNum(ref dwABPOS_EXT);
            KRS = dwABPOS_EXT["ABPAUFKRS"].ToString();
            LFD = dwABPOS_EXT["ABPAUFLFD"].ToString();
            POS = dwABPOS_EXT["ABPAUFPOS"].ToString();
            #region=====ABPOS=========
            EventDispatcher.Instance.RaiseStatus("转换ABPOS", StatusEventType.RunningProgress);
            SResult = Convert2ABPOS(dwABPOS_EXT);
            nwABPOS = SResult.Tables[0].Rows[0];
            DataRow dwABPOS_ITX = null;
            if (SetExt.Tables.Contains("ABPOS_ITX"))
            {
                SetExt.Tables.Remove("ABPOS_ITX");
            }
            DataTable tbABPOS_ITX = SResult.Tables[0];
            tbABPOS_ITX.TableName = "ABPOS_ITX";
            SetExt.Merge(tbABPOS_ITX);
            dwABPOS_ITX = SetExt.Tables["ABPOS_ITX"].Rows[0];
            if (newabk)
            {
                SResult = Convert2ABKOPF(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion

            #region=====ABPR=========
            EventDispatcher.Instance.RaiseStatus("转换ABPR", StatusEventType.RunningProgress);
            SResult = Convert2ABPR(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion

            #region=====ABAUFM=========
            EventDispatcher.Instance.RaiseStatus("转换Convert2ABAUFM", StatusEventType.RunningProgress);
            SResult = Convert2ABAUFMDI01(dwABPOS_ITX);
            DataRow dw001 = SResult.Tables[0].Rows[0];
            //Set4ITX.Merge(SResult);
            filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            if (rows.Length == 0)
            {
                Set4ITX.Merge(SResult);
            }
            foreach (DataRow row in rows)
            {
                dwABAUFM_ITX = row;
                SResult = NewReservaton(dw001, ref dwABAUFM_ITX, "DI");
                Set4ITX.Merge(SResult);
            }
            #endregion

            #region=====ABFOLGE=========
            ConsoldateFlow(dwABPOS_ITX, "DI");
            #endregion

            #region=====WDECK===========
            EventDispatcher.Instance.RaiseStatus("转换Convert2FDECKZ", StatusEventType.RunningProgress);
            SResult = Convert2WDECKZ(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion

            #region=====BAAUF===========
            //BAMANDANT BAAUFTYP BAAUFKRS BAAUFLFD
            filter = " ABAWAKZ='W' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            foreach (DataRow row in rows)
            {
                if (SetExt.Tables.Contains("ABAUFM_ITX"))
                {
                    SetExt.Tables["ABAUFM_ITX"].Clear();
                }
                tbABA = SetExt.Tables["ABAUFM_ITX"];
                DataRow rx = tbABA.NewRow();
                rx.ItemArray = row.ItemArray;
                tbABA.Rows.Add(rx);
                SetExt.Merge(tbABA);
                SResult = Convert2BAAUF732(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion

            #region=====GAAUF===========
            filter = " ABAWAKZ='G' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            foreach (DataRow row in rows)
            {
                if (SetExt.Tables.Contains("ABAUFM_ITX"))
                {
                    SetExt.Tables["ABAUFM_ITX"].Clear();
                }
                tbABA = SetExt.Tables["ABAUFM_ITX"];
                DataRow rx = tbABA.NewRow();
                rx.ItemArray = row.ItemArray;
                tbABA.Rows.Add(rx);
                SetExt.Merge(tbABA);
                SResult = Convert2GAAUF732(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion
            Set4ITX.Merge(SetExt.Tables["ABPOS_ITX"]);
        }
        public void CreateAB45(DataRow dwABPOS_EXT)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tbABA = null;
            bool newabk = false;
            DataRow nwABPOS;
            string VKPKRS = "  ", VKPLFD = "000000", VKPTYE = " ", VKPPOS = "000";
            string filter = "";
            DataRow[] rows;
            DataRow dwABAUFM_ITX = null;
            string KRS = "", LFD = "", POS = "";
            newabk = GetABPNum(ref dwABPOS_EXT);
            KRS = dwABPOS_EXT["ABPAUFKRS"].ToString();
            LFD = dwABPOS_EXT["ABPAUFLFD"].ToString();
            POS = dwABPOS_EXT["ABPAUFPOS"].ToString();
            if (SetExt.Tables.Contains("ABAUFM_ITX"))
            {
                tbABA = SetExt.Tables["ABAUFM_ITX"];
            }
            #region=====ABPOS=========
            EventDispatcher.Instance.RaiseStatus("转换ABPOS", StatusEventType.RunningProgress);
            SResult = Convert2ABPOS(dwABPOS_EXT);
            nwABPOS = SResult.Tables[0].Rows[0];
            DataRow dwABPOS_ITX = null;
            if (SetExt.Tables.Contains("ABPOS_ITX"))
            {
                SetExt.Tables.Remove("ABPOS_ITX");
            }
            DataTable tbABPOS_ITX = SResult.Tables[0];
            tbABPOS_ITX.TableName = "ABPOS_ITX";
            SetExt.Merge(tbABPOS_ITX);
            dwABPOS_ITX = SetExt.Tables["ABPOS_ITX"].Rows[0];
            if (newabk)
            {

                SResult = Convert2ABKOPF(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion
            #region=====ABPR=========
            EventDispatcher.Instance.RaiseStatus("转换ABPR", StatusEventType.RunningProgress);
            SResult = Convert2ABPR(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion

            #region=====ABFOLGE=========
            ConsoldateFlow(dwABPOS_ITX, "VW");
            #endregion

            #region=====BAAUF320=========
            SResult = Convert2BAAUF320(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion
            #region=====ABAUFM=========
            EventDispatcher.Instance.RaiseStatus("转换Convert2ABAUFM", StatusEventType.RunningProgress);
            SResult = Convert2ABAUFMVW01(dwABPOS_ITX);
            foreach (DataRow rw in SResult.Tables[0].Rows)
            {
                double Wnum = System.Math.Round(System.Convert.ToDouble(dwABPOS_ITX["ABPSOLLMENGE8"].ToString()) * 0.010, 0);
                if (Wnum == 0)
                    Wnum = 1;
                rw["ABASANZSTK"] = Wnum;
                double ABASSTKLG = System.Math.Round(System.Convert.ToDouble(dwABPOS_ITX["ABPSOLLMENGE1"].ToString()) * 10 / Wnum, 0);
                rw["ABASSTKLG"] = ABASSTKLG;
                rw["ABASSTKLGV"] = ABASSTKLG - 2000;
                rw["ABASSTKLGB"] = ABASSTKLG + 2000;
            }

            DataRow dw001 = SResult.Tables[0].Rows[0];
            //Set4ITX.Merge(SResult);
            filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            if (rows.Length == 0)
            {
                Set4ITX.Merge(SResult);
            }
            foreach (DataRow row in rows)
            {
                dwABAUFM_ITX = row;
                SResult = NewReservaton(dw001, ref dwABAUFM_ITX, "VW");
                Set4ITX.Merge(SResult);
                if (SetExt.Tables.Contains("ABAUFM_ITX"))
                {
                    SetExt.Tables["ABAUFM_ITX"].Clear();
                }
                tbABA = SetExt.Tables["ABAUFM_ITX"];
                DataRow rx = tbABA.NewRow();
                rx.ItemArray = row.ItemArray;
                tbABA.Rows.Add(rx);
                SetExt.Merge(tbABA);

                #region=====BABST====================
                decimal BAANZ1 = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE8"].ToString()) / 100;
                decimal WLeng = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE1"].ToString()) / 100;
                decimal ABPSOLLMENGE5 = Convert.ToDecimal(dwABPOS_ITX["ABPSOLLMENGE5"].ToString()) / 100;
                decimal BMSOLLLG, BMSOLLGEW;
                BMSOLLLG = System.Math.Round(WLeng / BAANZ1, 0);
                BMSOLLGEW = System.Math.Round(ABPSOLLMENGE5 * 10 / Convert.ToDecimal(BAANZ1), 0);
                DataTable tmpBABST = INTEXDB.BABST.Clone();
                string BKRS = dwABPOS_ITX["ABPSERIE"].ToString();
                string BLFD = "";
                for (int bi = 0; bi < BAANZ1; bi++)
                {
                    SResult = Convert2BABST(dwABPOS_ITX);
                    foreach (DataRow rbabst in SResult.Tables[0].Rows)
                    {
                        BLFD = GetAUFLFD(BKRS, "BABST");
                        rbabst["BMBAUMLFD"] = BLFD;
                    }
                    Set4ITX.Merge(SResult);
                }
                #endregion
            }
            #endregion
            #region=====GAAUF===========
            filter = " ABAWAKZ='G' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            foreach (DataRow row in rows)
            {
                if (SetExt.Tables.Contains("ABAUFM_ITX"))
                {
                    SetExt.Tables["ABAUFM_ITX"].Clear();
                }
                tbABA = SetExt.Tables["ABAUFM_ITX"];
                DataRow rx = tbABA.NewRow();
                rx.ItemArray = row.ItemArray;
                tbABA.Rows.Add(rx);
                SetExt.Merge(tbABA);
                SResult = Convert2GAAUF723(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion
            Set4ITX.Merge(SetExt.Tables["ABPOS_ITX"]);
        }
        public void CreateAB50(DataRow dwABPOS_EXT)
        {

            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tbNABPOS = new DataTable();
            tbNABPOS.TableName = "ABPOS";
            bool newabk = false;
            DataTable tbABA = null;
            string filter = "";
            DataRow[] rows;
            DataRow nwABPOS;

            newabk = false;
            string KRS = "", LFD = "", POS = "";
            newabk = GetABPNum(ref dwABPOS_EXT);
            KRS = dwABPOS_EXT["ABPAUFKRS"].ToString();
            LFD = dwABPOS_EXT["ABPAUFLFD"].ToString();
            POS = dwABPOS_EXT["ABPAUFPOS"].ToString();
            if (SetExt.Tables.Contains("ABAUFM_ITX"))
            {
                tbABA = SetExt.Tables["ABAUFM_ITX"];
            }
            #region=====ABPOS=========
            EventDispatcher.Instance.RaiseStatus("转换ABPOS", StatusEventType.RunningProgress);
            SResult = Convert2ABPOS(dwABPOS_EXT);
            nwABPOS = SResult.Tables[0].Rows[0];
            DataRow dwABPOS_ITX = null;
            if (SetExt.Tables.Contains("ABPOS_ITX"))
            {
                SetExt.Tables.Remove("ABPOS_ITX");
            }
            DataTable tbABPOS_ITX = SResult.Tables[0];
            tbABPOS_ITX.TableName = "ABPOS_ITX";
            SetExt.Merge(tbABPOS_ITX);

            dwABPOS_ITX = SetExt.Tables["ABPOS_ITX"].Rows[0];
            if (newabk)
            {
                SResult = Convert2ABKOPF(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion
            #region=====ABPR=========
            EventDispatcher.Instance.RaiseStatus("转换ABPR", StatusEventType.RunningProgress);
            SResult = Convert2ABPR(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion
            #region====ABAUFM=========
            DataRow dwABAUFM_ITX;
            EventDispatcher.Instance.RaiseStatus("转换Convert2ABAUFM", StatusEventType.RunningProgress);
            SResult = Convert2ABAUFMGP01(dwABPOS_ITX);
            DataRow dw001 = SResult.Tables[0].Rows[0];
            //Set4ITX.Merge(SResult);
            filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            if (rows.Length == 0)
            {
                Set4ITX.Merge(SResult);
            }
            foreach (DataRow row in rows)
            {
                dwABAUFM_ITX = row;
                SResult = NewReservaton(dw001, ref dwABAUFM_ITX, "GP");
                Set4ITX.Merge(SResult);
            }

            #endregion
            #region=====ABFOLGE=========
            ConsoldateFlow(dwABPOS_ITX, "GP");
            #endregion
            #region=====GAAUF310===========
            EventDispatcher.Instance.RaiseStatus("转换Convert2FDECKZ", StatusEventType.RunningProgress);
            SResult = Convert2GAAUF310(dwABPOS_ITX);
            Set4ITX.Merge(SResult);
            #endregion
            #region=====GAAUF===========
            filter = " ABPWAKZ='G' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
            rows = tbICC_ABAUFM.Select(filter);
            foreach (DataRow row in rows)
            {
                if (SetExt.Tables.Contains("ABAUFM_ITX"))
                {
                    SetExt.Tables["ABAUFM_ITX"].Clear();
                }
                tbABA = SetExt.Tables["ABAUFM_ITX"];
                DataRow rx = tbABA.NewRow();
                rx.ItemArray = row.ItemArray;
                tbABA.Rows.Add(rx);
                SetExt.Merge(tbABA);
                SResult = Convert2GAAUF713(dwABPOS_ITX);
                Set4ITX.Merge(SResult);
            }
            #endregion
            Set4ITX.Merge(SetExt.Tables["ABPOS_ITX"]);
        }
    }
}